package dao

import (
	"ginadmin/app/common"
	"ginadmin/app/config"
	"ginadmin/app/model"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

var (
	DB *gorm.DB
)

func InitMysql() {

	dsn := config.EnvConfig["DB_USER"] + ":" + config.EnvConfig["DB_PWD"] + "@tcp(" + config.EnvConfig["DB_HOST"] + ":" + config.EnvConfig["DB_PORT"] + ")/" + config.EnvConfig["DB_NAME"] + "?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.New(mysql.Config{

		DSN:               dsn,
		DefaultStringSize: 256, //设置string默认长度
	}), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			TablePrefix:   config.EnvConfig["DB_PREFIX"], //表前缀
			SingularTable: true,                          //禁用表名复数
		},
	})

	if err != nil {
		panic("msyql connect error")
	}

	DB = db

	model.ModelDb = DB

	//初始化表
	DB.AutoMigrate(&model.AdminUser{}, &model.AdminMenu{}, &model.AdminRole{})

	//初始化管理员
	var adminuser = &model.AdminUser{}
	DB.Where("id = ?", 1).First(adminuser)
	if adminuser.ID == 0 {
		//创建管理员
		adminuser.ID = 1
		adminuser.Username = "admin"
		adminuser.Password = common.Md5("123456")
		adminuser.Status = true
		adminuser.Roles = "1"

		DB.Create(adminuser)
	}

	//初始化超级管理员角色
	var role = &model.AdminRole{}
	DB.Where("id = ?", 1).First(role)
	if role.ID == 0 {

		role.ID = 1
		role.Name = "超级管理员"
		role.Rules = "*"

		DB.Create(role)
	}

	initMenu()

}

// 初始化菜单数据
func initMenu() {
	//初始化菜单数据
	var menuList [100]model.AdminMenu

	//var menuInfo = model.AdminMenu{}

	menuList[0] = model.AdminMenu{
		BaseModel: model.BaseModel{
			ID: 1,
		},
		Title:  "系统管理",
		Icon:   "layui-icon-util",
		Type:   false,
		IsShow: true,
		Sort:   0,
		Pid:    0,
		Href:   "/admin/system",
	}
	menuList[1] = model.AdminMenu{
		BaseModel: model.BaseModel{
			ID: 2,
		},
		Pid:    1,
		Title:  "系统设置",
		Icon:   "",
		Type:   true,
		IsShow: false,
		Sort:   0,
		Href:   "/admin/index/setting",
	}
	menuList[2] = model.AdminMenu{
		BaseModel: model.BaseModel{
			ID: 3,
		},
		Pid:    1,
		Title:  "菜单管理",
		Icon:   "",
		Type:   true,
		IsShow: true,
		Sort:   0,
		Href:   "/admin/menu/index",
	}
	menuList[3] = model.AdminMenu{
		BaseModel: model.BaseModel{
			ID: 4,
		},
		Pid:    1,
		Title:  "角色管理",
		Icon:   "",
		Type:   true,
		IsShow: true,
		Sort:   0,
		Href:   "/admin/role/index",
	}
	menuList[4] = model.AdminMenu{
		BaseModel: model.BaseModel{
			ID: 5,
		},
		Pid:    1,
		Title:  "管理员管理",
		Icon:   "",
		Type:   true,
		IsShow: true,
		Sort:   0,
		Href:   "/admin/admin/index",
	}

	menuList[5] = model.AdminMenu{
		BaseModel: model.BaseModel{
			ID: 6,
		},
		Pid:    3,
		Title:  "获取菜单列表",
		Icon:   "",
		Type:   true,
		IsShow: false,
		Sort:   0,
		Href:   "/admin/menu/getMenuList",
	}
	menuList[6] = model.AdminMenu{
		BaseModel: model.BaseModel{
			ID: 7,
		},
		Pid:    3,
		Title:  "添加/编辑菜单",
		Icon:   "",
		Type:   true,
		IsShow: false,
		Sort:   0,
		Href:   "/admin/menu/postMenu",
	}
	menuList[7] = model.AdminMenu{
		BaseModel: model.BaseModel{
			ID: 8,
		},
		Pid:    3,
		Title:  "删除菜单",
		Icon:   "",
		Type:   true,
		IsShow: false,
		Sort:   0,
		Href:   "/admin/menu/delMenu",
	}

	menuList[8] = model.AdminMenu{
		BaseModel: model.BaseModel{
			ID: 9,
		},
		Pid:    3,
		Title:  "选择菜单",
		Icon:   "",
		Type:   true,
		IsShow: false,
		Sort:   0,
		Href:   "/admin/menu/select",
	}

	menuList[9] = model.AdminMenu{
		BaseModel: model.BaseModel{
			ID: 10,
		},
		Pid:    4,
		Title:  "获取角色列表",
		Icon:   "",
		Type:   true,
		IsShow: false,
		Sort:   0,
		Href:   "/admin/role/getList",
	}

	menuList[10] = model.AdminMenu{
		BaseModel: model.BaseModel{
			ID: 11,
		},
		Pid:    4,
		Title:  "添加/编辑角色",
		Icon:   "",
		Type:   true,
		IsShow: false,
		Sort:   0,
		Href:   "/admin/role/post",
	}

	menuList[11] = model.AdminMenu{
		BaseModel: model.BaseModel{
			ID: 12,
		},
		Pid:    4,
		Title:  "删除角色",
		Icon:   "",
		Type:   true,
		IsShow: false,
		Sort:   0,
		Href:   "/admin/role/delete",
	}

	menuList[12] = model.AdminMenu{
		BaseModel: model.BaseModel{
			ID: 13,
		},
		Pid:    4,
		Title:  "获取选择菜单",
		Icon:   "",
		Type:   true,
		IsShow: false,
		Sort:   0,
		Href:   "/admin//role/getMenu",
	}

	menuList[13] = model.AdminMenu{
		BaseModel: model.BaseModel{
			ID: 14,
		},
		Pid:    5,
		Title:  "管理员列表",
		Icon:   "",
		Type:   true,
		IsShow: false,
		Sort:   0,
		Href:   "/admin/admin/getList",
	}
	menuList[14] = model.AdminMenu{
		BaseModel: model.BaseModel{
			ID: 15,
		},
		Pid:    5,
		Title:  "管理员添加/编辑",
		Icon:   "",
		Type:   true,
		IsShow: false,
		Sort:   0,
		Href:   "/admin/admin/post",
	}

	menuList[15] = model.AdminMenu{
		BaseModel: model.BaseModel{
			ID: 16,
		},
		Pid:    5,
		Title:  "管理员删除",
		Icon:   "",
		Type:   true,
		IsShow: false,
		Sort:   0,
		Href:   "/admin/admin/delete",
	}

	for _, v := range menuList {

		var minfo model.AdminMenu
		if v.BaseModel.ID > 0 {
			DB.Where("id = ?", v.BaseModel.ID).First(&minfo)

			vv := v
			if minfo.ID == 0 {
				//创建菜单
				DB.Create(&vv)
			}
		}

	}
}

func Close() {

	db, _ := DB.DB()

	db.Close()
}
